<%@ page language="java" contentType="text/javascript; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> 
<%@ page isELIgnored="false"%>
<%
	tw.rescue.lib.JspUtil.setup(request);
%>
jQuery(document).ready(function() {
<c:if test="${isBeforeIE7}">
	jQuery.ajaxSetup({cache:false});
	window.onscroll = function(e) {
		var top = jQuery('html,body').scrollTop();
		var height = jQuery('html,body').height();
<%--	if (document.body.scrollTop)
			top = document.body.scrollTop;
		else if (document.documentElement.scrollTop)
			top = document.documentElement.scrollTop;--%>
		var e = document.getElementById('inputMask');
		if (e) {e.style.top = '0px'; e.style.height = (top+height-30) + 'px';} 
		e = E('jq_message');
		if (e) e.style.top = (top+30) + 'px';
		e = E('diag_mask');
		if (e) {e.style.top = '0px'; e.style.height = (top+height-30) + 'px';} 
		e = E('diag_bg');
		if (e) e.style.top = (top+30) + 'px';
	}
</c:if>
	var fs = document.getElementsByTagName('form');
	if (fs!=null) {
		for (var i=0; i<fs.length; i++) {
			fs[i].innerHTML = '<input type="hidden" name="${JSON_REQUEST_HIDDEN_NAME}" value="1"/>' + fs[i].innerHTML;
		}
	}
	createPopup();
	if (typeof onReady == 'function') onReady();
});

var __jsonRedirect__ = '';

function scrollTop() {
	return filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function filterResults(w, d, b) {
	var r = w ? w : 0;
	if (d && (!r || (r > d)))
		result = d;
	return b && (!r || (r > b)) ? b : r;
}

function E(id) { return document.getElementById(id); }
function S(id) { return E(id).style; }

function timeNotice(notice, millis) {
	showPopup(notice, null, millis);
}

function showNotice(notice) {
	showPopup(notice, null, 2500);
}

function showError(error) {
	showPopup(error, '#fef', -1);
}

function createPopup() {
	if (E('jq_message')!=null) return;
    var e = document.getElementsByTagName('body');
    if (!e) return;
    e = e[0];
    e.innerHTML = '<div id="jq_message"><img id="jq_message_close" src="${ctxPath}/${template}/image/cross.png" onclick="hidePopup()"/><span id="jq_message_text">test</span></div>' + e.innerHTML;
}

function showPopup(message, bgColor, closeAfterMillis) {
    if (message==null || message.length==0 || closeAfterMillis==0) return;
	var p = jQuery('#jq_message');
	var top = 30;
/*@cc_on
@if (@_jscript_version <= 5.6)
  top += jQuery('html,body').scrollTop();
/*@end @*/
	var left = jQuery('html,body').scrollLeft() + (jQuery(window).width()-p.width())/2;
	if (!bgColor) bgColor = '#ffe';
	p.css({"left":left, "top":top, "background-color":bgColor}).fadeIn(500);
	jQuery('#jq_message_text').html(message);
	if (closeAfterMillis>0)
		setTimeout('hidePopup()', closeAfterMillis, 'javascript');
}

function hidePopup() {
	jQuery('#jq_message').fadeOut('slow');
	doRedirect();
}

function doRedirect() {
	var url = __jsonRedirect__;
	__jsonRedirect__ = null;
	if (url&&url.length>0) {
	  if (url.indexOf('()')>=0)
	    eval(url);
	  else
		location.href = url;
	}
}

function jsonUpload(form, timeout) {
	jsonPostForm(form, true, timeout);
	return false;
}

function jsonSubmit(form, timeout) {
	jsonPostForm(form, false, timeout);
	return false;
}

function jsonPostForm(form, isUpload, timeout) {
	inputOff(); <%--document.body.style.cursor = 'wait';--%>
	jQuery('#jq_message').fadeOut('fast');
	if (timeout==null) timeout=30000;
	var contentType = isUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
	jQuery('#'+form.id).jsonSubmit({
		cache: false,
		contentType: contentType,
		url: form.action,
		type: 'POST',
		dataType: 'json',
		timeout: timeout, // GAE request timeout limit
		error: function(){
			inputOn(); <%--document.body.style.cursor = 'default';--%>
			alert('<bean:message key="errors.ajax.submit"/>');
		},
		success: function(json){
			__jsonRedirect__ = null;
			if (top.ajaxSuccessListener) top.ajaxSuccessListener(json, form);
			inputOn(); <%--document.body.style.cursor = 'default';--%>
			if (json!=null) {
			  __jsonRedirect__ = json.redirect;
			  if (json.status==0) {
			    if (redirectNow(json)) {
			       doRedirect();
			       return;
			    }
				timeNotice(json.object, json.timer);
			  }
			  else
				showError(json.object);
			}
		},
		iframe: isUpload
	});
	return false;
}

function ajaxHtml(form, elementId) {
	inputOff();
	var contentType = 'application/x-www-form-urlencoded';
	jQuery('#'+form.id).jsonSubmit({
		cache: false,
		contentType: contentType,
		url: form.action,
		type: 'POST',
		dataType: 'html',
		timeout: 30000, // GAE request timeout limit
		error: function(){
			inputOn();
			alert('<bean:message key="errors.ajax.submit"/>');
		},
		success: function(responseHtml) {
			__jsonRedirect__ = null;
			if (top.ajaxSuccessListener) top.ajaxSuccessListener(responseHtml, form);
			inputOn();
			var url = parseRedirect(responseHtml);
			if (url!=null) {
				location.href = url;
				return;
			}
			jQuery('#'+elementId).html(responseHtml);
		},
		iframe: false
	});
	return false;
}

function parseRedirect(html) {
	if (html.indexOf('{"')==0) {
		_json = null;
		eval('_json='+html);
		if (_json!=null&&_json.redirect!=null&&_json.redirect.length>0)
			return _json.redirect;
	}
	return null;
}

function ajaxSyncLoad(url, elementId) {
	inputOff();
	var contentType = 'application/x-www-form-urlencoded';
	jQuery.ajax({
		async : false,
		contentType: contentType,
		url: url,
		type: 'GET',
		dataType: 'html',
		timeout: 30000, // GAE request timeout limit
		error: function(){
			inputOn();
			alert('<bean:message key="errors.ajax.submit"/>');
		},
		success: function(responseHtml) {
			inputOn();
			jQuery('#'+elementId).html(responseHtml);
		}
	});
	return false;
}

function inputOff() {
  var e=E('inputMask');
  if (e) e.style.display='';
}

function inputOn() {
  var e=E('inputMask');
  if (e) e.style.display='none';
}

function getJsonText(json, defText) {
	if (json==null || json.object==null || json.object.length==0)
		return defText;
	return json.object;
}

function redirectNow(json) {
    if (json==null) return false;
    var o = json.object;
    var u = json.redirect;
    return (u && u.length>0 && (o==null || o.length==0))
}

function isPagingHyperlink(a, pattern) {
    return a.href && a.href.indexOf('javascript')<0 && a.href.indexOf(pattern)>=0;
}

function showPagingHyperlink(a, br) {
	var e = a.parentNode;
	if (br) e.innerHTML = '<br/>' + e.innerHTML;
    e.className = 'visPagelinks';
}

function unAmp(u) {
    return u.replace(/\&amp\;/g, "&");
}

function unSQ(s) {
    return s.replace(/\&#39\;/g, "'");
}


function initJqModal() {
	if (window.jQuery && jQuery.ui)
		jQuery('#jqui_diag').dialog({autoOpen:false, modal:true, closeOnEscape:false, resizable:false});
}
function showJqModal(title, url) {
	loadJQUI();
<c:if test="${not isBeforeIE7}">
	if (window.jQuery && jQuery.ui)
		jQuery('#jqui_diag').html('').load(url).dialog('open').dialog('option', 'title', title).dialog('option', 'position', ['center', 30]);
</c:if>		
<c:if test="${isBeforeIE7}">
	if (window.jQuery && jQuery.ui) {
		jQuery.ajaxSetup({async:false});
		jQuery('#jqui_diag').load(url).dialog('open').dialog('option', 'title', title).dialog('option', 'position', ['center', 30]);
		jQuery.ajaxSetup({async:true});
	}
</c:if>		
}

function hideJqModal() {
	if (window.jQuery && jQuery.ui)
		jQuery('#jqui_diag').dialog('close');
}

function loadJQUI() {
<c:if test="${not isBeforeIE7}">
 if (window.jQuery && jQuery.ui) return;
// var css = createCss('${jqueryUiCssPath}');
// var h = document.getElementsByTagName("HEAD")[0];
// h.appendChild(css);
 jQuery.ajaxSetup({async:false});
 jQuery.getScript('${jqueryUiJsPath}');
 jQuery.ajaxSetup({async:true});
</c:if>
 initJqModal();
}

function createJs(src) {
 var js = document.createElement("script");
 js.setAttribute("type","text/javascript");
 js.setAttribute("src", src);
 return js;
}

function createCss(href) {
 var css = document.createElement("link");
 css.setAttribute("rel", "stylesheet");
 css.setAttribute("type", "text/css");
 css.setAttribute("href", href);
 return css;
}
